// 225/3/28
// 买卖股票的最佳时机

#include <iostream>
using namespace std;

int main() {
    int n, ans = 0; cin >> n;
    int lowest = 10001, cur = 0, pre = 0;
    while(n--)
    {
        cin >> cur;
        if(cur < pre)
        {
            ans += pre - lowest;
            lowest = cur;
        }
        else if(cur < lowest)
            lowest = cur;
        pre = cur;
    }
    ans += pre - lowest;
    cout << ans << endl;
}
// 64 位输出请用 printf("%lld")